package com.example.demo.controller;

import com.example.demo.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

//@Controller
//@ResponseBody
@RestController//是上面两个注解的复合注解
@RequestMapping("/test")
@Slf4j
public class TestController {

//    @RequestMapping(value = "hi",method = RequestMethod.POST)
//    @PostMapping("/hi")
//    @RequestMapping("hi")
    @GetMapping("/hi")
    public String sayHi(String name){
        return "hi,"+name;
    }

    @RequestMapping("/num")
    public String num(Integer num){
        return "num="+num;
    }

    @GetMapping("/show-user")
    public String showUser(User user){
        return user.toString();
    }
    @RequestMapping("/show-time")
    public String showTime(@RequestParam("t") String startTime){
        return "开始时间："+startTime;
    }

    //加@RequestBody才能从前端获取到JSON对象
    @RequestMapping("/show-json")//post请求
    public String showJson(@RequestBody User user){
        return user.toString();
    }

    @RequestMapping("/login/{username}/{password}")
    public String login(@PathVariable String username,@PathVariable String password){
        return "你好: "+username;
    }

    @RequestMapping("/upfile")
    public String upFile(@RequestPart ("myfile") MultipartFile file) throws IOException {
        String path="D:\\tmp\\img.png";
        file.transferTo(new File(path));
        return path;
    }

    @RequestMapping("/myupfile")
    public String myUpFile(@RequestPart ("myfile") MultipartFile file) throws IOException {
        //根目录
        String path="D:\\tmp\\";
        //根目录+【唯一文件名】
        path+= UUID.randomUUID().toString().replace("-","");
        //根目录+唯一文件名+【.后缀】,获取最后一个小数点之后的
        path+=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        file.transferTo(new File(path));

        return path+"上传成功！";
    }

    @RequestMapping("getpara")
    public String getParam(HttpServletRequest request){
        String name=request.getParameter("name");
        return name;
    }

    @RequestMapping("getck")
    public String getck(HttpServletRequest request){
        Cookie cookies[]=request.getCookies();
        for(Cookie item:cookies){
            log.error(item.getName()+" : "+item.getValue());
        }
        return "cookies~~";
    }

    @RequestMapping("/getck2")
    public String getck2(@CookieValue ("zhangsan") String value){
        return value;
    }

    @RequestMapping("/getsess")
    public String getSession(HttpServletRequest request){
        HttpSession session= request.getSession(false);
        if(session!=null&&session.getAttribute("userinfo")!=null){
            return (String) session.getAttribute("userinfo");
        }else{
            return "暂无session信息";
        }
    }

    @RequestMapping("/getsess2")
    public String getSession2(@SessionAttribute(value="userinfo",required = false) String value){
        return "session: "+value;
    }

    @RequestMapping("/setsess")
    public String setSession(HttpServletRequest request){
        HttpSession session=request.getSession();
        if(session!=null){
            session.setAttribute("userinfo","userinfo");
        }
        return "设置成功！";
    }

}
